import sys
import random

def f(n):
    lst=[]
    for i in range(8):
        lst.append(n)
        if n==6174:
            break
        nlst=list(map(int,str(n)))
        nmax=sorted(nlst,reverse=True)
        nmax=int(''.join(list(map(str,nmax))))
        nmin=sorted(nlst)
        nmin=int(''.join(list(map(str,nmin))))
        n=nmax-nmin
    else:
        lst.append('wrong')
    return lst

a=[6175,6174,7164,5123,7890,9087,4321,9321,2749,1234]
for i  in range(10):
    fin = open('{}.in'.format(i+1),'w')
    fout = open('{}.out'.format(i+1),'w')
    print(a[i],file=fin)
    print(*f(a[i]),file=fout)
    fin.close()
    fout.close()
